package com.hckjsec.cloud.controller;

import java.util.ArrayList;
import java.util.List;

import com.hckjsec.cloud.domain.wechatDataView.CurrencyView;
import com.hckjsec.common.utils.DateUtils;
import com.hckjsec.common.utils.SecurityUtils;
import com.hckjsec.common.utils.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.hckjsec.common.annotation.Log;
import com.hckjsec.common.core.controller.BaseController;
import com.hckjsec.common.core.domain.AjaxResult;
import com.hckjsec.common.enums.BusinessType;
import com.hckjsec.cloud.domain.WebApplicationConfig;
import com.hckjsec.cloud.service.IWebApplicationConfigService;
import com.hckjsec.common.utils.poi.ExcelUtil;
import com.hckjsec.common.core.page.TableDataInfo;

/**
 * 公司网站主Controller
 * 
 * @author cyt
 * @date 2021-09-22
 */
@RestController
@RequestMapping("/web/internet")
public class WebApplicationConfigController extends BaseController
{
    @Autowired
    private IWebApplicationConfigService webApplicationConfigService;

    /**
     * 查询公司网站主列表
     */
    @GetMapping("/list")
    public TableDataInfo list(WebApplicationConfig webApplicationConfig)
    {
        startPage();
        if(!SecurityUtils.isAdmin(SecurityUtils.getUserId())){
            webApplicationConfig.setDeptId(SecurityUtils.getDeptId());
        }
        List<WebApplicationConfig> list = webApplicationConfigService.selectWebApplicationConfigList(webApplicationConfig);
        return getDataTable(list);
    }

    /**
     * 导出公司网站主列表
     */
    @Log(title = "公司网站主", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(WebApplicationConfig webApplicationConfig)
    {
        if(!SecurityUtils.isAdmin(SecurityUtils.getUserId())){
            webApplicationConfig.setDeptId(SecurityUtils.getDeptId());
        }
        List<WebApplicationConfig> list = webApplicationConfigService.selectWebApplicationConfigList(webApplicationConfig);
        ExcelUtil<WebApplicationConfig> util = new ExcelUtil<WebApplicationConfig>(WebApplicationConfig.class);
        return util.exportExcel(list, "config");
    }

    /**
     * 获取公司网站主详细信息
     */
    @GetMapping(value = "/getset/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(webApplicationConfigService.selectWebApplicationConfigById(id));
    }

    /**
     * 新增公司网站主
     */
    @Log(title = "公司网站主", businessType = BusinessType.INSERT)
    @PostMapping("/addset")
    public AjaxResult add(@RequestBody WebApplicationConfig webApplicationConfig)
    {
        int rows =0;
        if (StringUtils.isNull(webApplicationConfig.getId())){
            rows=webApplicationConfigService.insertWebApplicationConfig(webApplicationConfig);
        }else{
            webApplicationConfig.setUpdatedAt(DateUtils.parseDate(DateUtils.getTime()));
            rows=webApplicationConfigService.updateWebApplicationConfig(webApplicationConfig);
        }
        return toAjax(rows);
    }
    /**
     * 删除公司网站主
     */
    @Log(title = "公司网站主", businessType = BusinessType.DELETE)
	@DeleteMapping("/delset/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(webApplicationConfigService.deleteWebApplicationConfigByIds(ids));
    }


    /**
     * 获取站点APPid
     */
    @GetMapping("/getInternetAppid")
    public AjaxResult getInternetAppid(WebApplicationConfig webApplicationConfig)
    {
        if(!SecurityUtils.isAdmin(SecurityUtils.getUserId())){
            webApplicationConfig.setDeptId(SecurityUtils.getDeptId());
        }
        List<CurrencyView> currencyViewList = new ArrayList<>();
        List<WebApplicationConfig> list = webApplicationConfigService.selectWebApplicationConfigList(webApplicationConfig);
        list.forEach(kv->{
            CurrencyView currencyView = new CurrencyView();
            currencyView.setKey(kv.getName());
            currencyView.setValue(kv.getAppid());
            currencyViewList.add(currencyView);
        });
        return AjaxResult.success(currencyViewList);
    }
}
